package com.ydgk.servlet;

import com.ydgk.bean.User;
import com.ydgk.service.RecordService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Map;
import java.util.Set;

@WebServlet(name = "BaseServlet", urlPatterns = "/BaseServlet.action")
public class BaseServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Map<String , String[]>  map = request.getParameterMap();
        Set<String> key = map.keySet();
        System.out.println("--------提交给servlet的参数开始----------");
        System.out.println("请求方式:"+request.getMethod());
        System.out.println("请求服务器:"+request.getRequestURI());
        for (String k : key) {
            System.out.print( k + "=" );
            String []value = map.get(k);
            for (String v : value) {
                System.out.println(v);
            }
        }
        System.out.println("--------提交给servlet的参数结束----------");

        String flag = request.getParameter("flag");//add
        HttpSession session=request.getSession();
        if(session.getAttribute("user")!=null){
            RecordService recordService = new RecordService();
            User user=(User) session.getAttribute("user");
            recordService.insert(user.getUid(),flag);
        }
        Class claxx = this.getClass();

        try {

            Method method = claxx.getDeclaredMethod(flag , HttpServletRequest.class , HttpServletResponse.class);//得到要执行的方法对象

            method.setAccessible(true);//取消访问查询

            method.invoke(this,request,response);//执行此方法

        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }


}
